﻿namespace EFS.RC.CCAdmin.Skills
{
	/// Class List
	public partial class List : EFS.RC.CCAdmin.Common.Control
	{
		#region Constructors
		/// Implicit
		public List()
		{
			InitializeComponent();

			// ignore in designer
			if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				//set listener up for list load
				UtilitiesService.Skills_GetAllCompleted += new System.EventHandler<WebProxy.Skills_GetAllCompletedEventArgs>(UtilitiesService_Skills_GetAllCompleted);

				//set delete response listener
				UtilitiesService.Skill_DeleteCompleted += new System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(UtilitiesService_Skill_DeleteCompleted);
			}
		}
		#endregion

		#region LoadData

		/// LoadSkills
		private void LoadSkills()
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Please wait while retrieving data");

			//call ws
			UtilitiesService.Skills_GetAllAsync();
		}
		#endregion

		#region WS callbacks
		/// Get all skills completed
		private void UtilitiesService_Skills_GetAllCompleted(object sender, WebProxy.Skills_GetAllCompletedEventArgs e)
		{
			// hide wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			// check
			if (null != e.Error)
			{
				ShowErrorMessage("Failed to retrieve skills.", e.Error);

				// done
				return;
			}

			// remove previous
			dataGridSkills.ItemsSource = null;

			// check
			if (null == e.Result)
			{
				// done
				return;
			}

			try
			{
				// bind/fill form
				dataGridSkills.ItemsSource = EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.Deployment.Entities.Skill[]>(e.Result.ToString()); ;
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to retrieve data.", ex);
			}
		}

		/// Delete skill completed
		private void UtilitiesService_Skill_DeleteCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			if (null != e.Error)
			{
				ShowErrorMessage("Failed to delete skill.", e.Error);
			}
			else
			{
				LoadSkills();
			}
		}
		#endregion

		#region CRUD Click listeners

		//edit skill button click in datagrid
		private void EditLink_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			EFS.RC.CC.Deployment.Entities.Skill skill = (EFS.RC.CC.Deployment.Entities.Skill) dataGridSkills.SelectedItem;

			EFS.Common.UI.Controls.ModalDialogHost wnd = new EFS.Common.UI.Controls.ModalDialogHost();
			wnd.HostedContent = new EFS.RC.CCAdmin.Skills.Edit(wnd, skill.ID);
			wnd.Show(OnEditWindowClosed);
		}

		//delete button click in datagrid
		private void DeleteLink_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			if (System.Windows.Browser.HtmlPage.Window.Confirm("Permanently delete skill?"))
			{
				// show wait DLG
				EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Please wait while deleting skill");

				EFS.RC.CC.Deployment.Entities.Skill skill = (EFS.RC.CC.Deployment.Entities.Skill)dataGridSkills.SelectedItem;
				UtilitiesService.Skill_DeleteAsync(skill.ID.Value);
			}
		}

		#endregion

		#region Window callbacks
		//add new skill
		private void OnNewWindowClosed(EFS.Common.UI.Controls.ModalDialogHost host)
		{
			//x or cancel pressed
			if (host.DialogClosedReason != EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok)
			{
				return;
			}

			// load skills
			LoadSkills();
		}

		//update skill
		private void OnEditWindowClosed(EFS.Common.UI.Controls.ModalDialogHost host)
		{
			//x or cancel pressed
			if (host.DialogClosedReason != EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok)
			{
				return;
			}

			// load skills
			LoadSkills();
		}
		#endregion

		#region Events
		/// Tool bar click
		private void ToolbarItem_New_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			EFS.Common.UI.Controls.Toolbar.ToolbarItem toolbarItem = (EFS.Common.UI.Controls.Toolbar.ToolbarItem)sender;

			EFS.Common.UI.Controls.ModalDialogHost wnd = new EFS.Common.UI.Controls.ModalDialogHost();
			wnd.HostedContent = new EFS.RC.CCAdmin.Skills.Edit(wnd, null);
			wnd.Show(OnNewWindowClosed);
		}

		/// Loaded
		private void Control_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			//load data
			LoadSkills();
		}
		#endregion
	}

}